package model

import (
	"fmt"
	"ginblog/utils"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/postgres"
)

var db *gorm.DB
var err error

func InitDB() {
	args := fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s  sslmode=disable",
		utils.DbHost,
		utils.DbPort,
		utils.DbUser,
		utils.DbName,
		utils.DbPassword)
	db, err = gorm.Open("postgres", args)
	if err != nil {
		fmt.Printf("数据库连接失败", err)
		return
	}
	_ = db.AutoMigrate(&User{}, &Article{}, &Category{})
	db.AutoMigrate(&Comment{})

}
